//
//  SiteManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-25.
//
//

#import "SiteManager.h"
#import "ServerCommunicationManager.h"

#import "QryAllSiteInfoRequest.h"
#import "QryAllSiteInfoResponse.h"

#import "BuySiteTicketRequest.h"
#import "BuySiteTicketResponse.h"


@implementation SiteManager

static SiteManager* gManager = nil;


+ (SiteManager*)shareManager
{
    if (gManager == nil) {
        gManager = [[SiteManager alloc] init];
    }
    return gManager;
}

//查询所有门票
- (void)searchAllSiteInfo:(NSString*)sceneType
{
    QryAllSiteInfoRequest *request = [[QryAllSiteInfoRequest alloc] initQryAllSiteInfo:self sceneType:sceneType];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

//购买门票
- (void)buySiteTicket:(NSString*)reserveName
          reserveDate:(NSString*)reserveDate
        reserveNumber:(NSString*)reserveNumber
          mobilephone:(NSString*)mobilephone
        sceneTicketId:(NSString*)sceneTicketId
           mobileType:(NSString*)mobileType
              webTime:(NSString*)webTime
             allPrice:(int)allPrice
{
    BuySiteTicketRequest *request = [[BuySiteTicketRequest alloc] buySiteTicket:self
                                                                    reserveName:reserveName
                                                                    reserveDate:reserveDate
                                                                  reserveNumber:reserveNumber
                                                                    mobilephone:mobilephone
                                                                  sceneTicketId:sceneTicketId
                                                                     mobileType:mobileType
                                                                        webTime:webTime
                                                                       allPrice:allPrice];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}



- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[QryAllSiteInfoResponse class]])
    {
        [self handleQryAllSiteInfoSuccess:(QryAllSiteInfoResponse*)response];
    }
    else if ([response isKindOfClass:[BuySiteTicketResponse class]])
    {
        [self handleBuySiteTicketSuccess:(BuySiteTicketResponse*)response];
    }
    
}

- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[QryAllSiteInfoResponse class]])
    {
        [self.delegate qryAllSiteInfo:NO AllSiteInfo:nil TicketInfoArray:nil];
    }
    if ([response isKindOfClass:[BuySiteTicketResponse class]])
    {
        [self.delegate buySiteTicket:NO tn:nil];
    }
    
}


//查看所有景区票
- (void)handleQryAllSiteInfoSuccess:(QryAllSiteInfoResponse*)response
{
    NSMutableArray *siteInfoArray = response.siteInfoArray;
    NSMutableArray *tiketInfoArray = response.ticketInfoArray;
    if (siteInfoArray && tiketInfoArray)
    {
        [self.delegate qryAllSiteInfo:YES AllSiteInfo:siteInfoArray TicketInfoArray:tiketInfoArray];
    }
    else
    {
        [self.delegate qryAllSiteInfo:NO AllSiteInfo:nil TicketInfoArray:nil];
    }
}

//购买景区票
- (void)handleBuySiteTicketSuccess:(BuySiteTicketResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate buySiteTicket:YES tn:tn];
    }
    else
    {
        [self.delegate buySiteTicket:NO tn:nil];
    }
}

@end
